<?php
class My_Globals
{
	// Define static variables
	private static $_configuation = null;
	private static $_storage = null;
	
	/*
	 * Get configuation
	 */
	public static function getConfiguration()
	{
		if(self::$_configuation == null) {
			self::$_configuation = Zend_Registry::get(APP_CONFIGURATION);
		}
		return self::$_configuation;	
	}
	
	public static function getStorage()
	{
		if(self::$_storage == null) {
			$options = self::getConfiguration()->storage->mysql->toArray();
			$options['params']['driver_options'] = array(
				MYSQLI_INIT_COMMAND => 'SET NAMES utf8;'
			);
			
			// Connect to DB
			self::$_storage = Zend_Db::factory($options['adapter'], $options['params']);
			
			// Change fetch mode 
			self::$_storage->setFetchMode(Zend_Db::FETCH_ASSOC);
			
			// Set default apdater for Db_Table 
			Zend_Db_Table::setDefaultAdapter(self::$_storage);
			
			// Set enable profiler
			self::$_storage->getProfiler()->setEnabled(true);
			
			// Clean
			unset($options);
		}
		return self::$_storage; 
	}
	
	public static function utf8ToAscii($tring, $bit='-') 
	{
        $utf8 = array(
            'ấ' => 'a',
            'ầ' => 'a',
            'ẩ' => 'a',
            'ẫ' => 'a',
            'ậ' => 'a',
            'Ấ' => 'a',
            'Ầ' => 'a',
            'Ẩ' => 'a',
            'Ẫ' => 'a',
            'Ậ' => 'a',
            'ắ' => 'a',
            'ằ' => 'a',
            'ẳ' => 'a',
            'ẵ' => 'a',
            'ặ' => 'a',
            'Ắ' => 'a',
            'Ằ' => 'a',
            'Ẳ' => 'a',
            'Ẵ' => 'a',
            'Ặ' => 'a',
            'á' => 'a',
            'à' => 'a',
            'ả' => 'a',
            'ã' => 'a',
            'ạ' => 'a',
            'â' => 'a',
            'ă' => 'a',
            'Á' => 'a',
            'À' => 'a',
            'Ả' => 'a',
            'Ã' => 'a',
            'Ạ' => 'a',
            'Â' => 'a',
            'Ă' => 'a',
            'ế' => 'e',
            'ề' => 'e',
            'ể' => 'e',
            'ễ' => 'e',
            'ệ' => 'e',
            'Ế' => 'e',
            'Ề' => 'e',
            'Ể' => 'e',
            'Ễ' => 'e',
            'Ệ' => 'e',
            'é' => 'e',
            'è' => 'e',
            'ẻ' => 'e',
            'ẽ' => 'e',
            'ẹ' => 'e',
            'ê' => 'e',
            'É' => 'e',
            'È' => 'e',
            'Ẻ' => 'e',
            'Ẽ' => 'e',
            'Ẹ' => 'e',
            'Ê' => 'e',
            'í' => 'i',
            'ì' => 'i',
            'ỉ' => 'i',
            'ĩ' => 'i',
            'ị' => 'i',
            'Í' => 'i',
            'Ì' => 'i',
            'Ỉ' => 'i',
            'Ĩ' => 'i',
            'Ị' => 'i',
            'ố' => 'o',
            'ồ' => 'o',
            'ổ' => 'o',
            'ỗ' => 'o',
            'ộ' => 'o',
            'Ố' => 'o',
            'Ồ' => 'o',
            'Ổ' => 'o',
            'Ô' => 'o',
            'Ộ' => 'o',
            'ớ' => 'o',
            'ờ' => 'o',
            'ở' => 'o',
            'ỡ' => 'o',
            'ợ' => 'o',
            'Ớ' => 'o',
            'Ờ' => 'o',
            'Ở' => 'o',
            'Ỡ' => 'o',
            'Ợ' => 'o',
            'ó' => 'o',
            'ò' => 'o',
            'ỏ' => 'o',
            'õ' => 'o',
            'ọ' => 'o',
            'ô' => 'o',
            'ơ' => 'o',
            'Ó' => 'o',
            'Ò' => 'o',
            'Ỏ' => 'o',
            'Õ' => 'o',
            'Ọ' => 'o',
            'Ô' => 'o',
            'Ơ' => 'o',
            'ứ' => 'u',
            'ừ' => 'u',
            'ử' => 'u',
            'ữ' => 'u',
            'ự' => 'u',
            'Ứ' => 'u',
            'Ừ' => 'u',
            'Ử' => 'u',
            'Ữ' => 'u',
            'Ự' => 'u',
            'ú' => 'u',
            'ù' => 'u',
            'ủ' => 'u',
            'ũ' => 'u',
            'ụ' => 'u',
            'ư' => 'u',
            'Ú' => 'u',
            'Ù' => 'u',
            'Ủ' => 'u',
            'Ũ' => 'u',
            'Ụ' => 'u',
            'Ư' => 'u',
            'ý' => 'y',
            'ỳ' => 'y',
            'ỷ' => 'y',
            'ỹ' => 'y',
            'ỵ' => 'y',
            'Ý' => 'y',
            'Ỳ' => 'y',
            'Ỷ' => 'y',
            'Ỹ' => 'y',
            'Ỵ' => 'y',
            'đ' => 'd',
            'Đ' => 'd',
            ' ' => $bit,
            '/' => $bit,
            '&' => $bit,
            '.' => $bit,
            '---' => $bit,
            '--' => $bit,
            '%20' => $bit
        );
        return str_replace(array_keys($utf8), array_values($utf8), $tring);
    }
    
	public static function randNumber($min, $max, $seed = -1) {
        if (!defined('RAND_SEEDED')) {
            if ($seed == -1) {
                $seed = (double) microtime() * 1000000;
            }
            mt_srand($seed);
            define('RAND_SEEDED', true);
        }
        return mt_rand($min, $max);
    }
    
	public static function randWord($number) {
        $arrChar = 'abRSTUcdefghEFGHnrls456L78tiuxMyzIABCN1vwPQVjkmWXYZ12pq3DJK9';
        $string = '';
        $arrCharLength = strlen($arrChar) - 1;
        for ($j = 0; $j < $number; $j++) {
            $string .= substr($arrChar, self::randNumber(0, $arrCharLength), 1);
        }
        return $string;
    }
    
	public static function cutString($str, $start, $len, $charset='UTF-8') {
        //Neu la chuoi rong
        if (empty($str)) {
            return '';
        }
        //Kiem tra do dai chuoi
        if (iconv_strlen($str, $charset) <= $len) {
            return $str;
        }
        //Tien hanh cat chuoi
        $str = html_entity_decode($str, ENT_QUOTES, $charset);
        $str = iconv_substr($str, $start, $len, $charset);
        return $str . "...";
    }
    
    public static function initData($fields, $data) 
    {
    	$rs = array();
    	if(is_array($fields) && is_array($data)) {
    		foreach($fields as $field) {
    			if(isset($data[$field])) {
    				$rs[$field] = $data[$field];
    			}
    		}
    	}
    	return $rs;
    }
    
	public static function aliasCreator($string) {
        //remove any '-' from the string they will be used as concatonater
        $string = str_replace('-', ' ', trim($string));
        $string = self::transliterate($string);
        // remove any duplicate whitespace, and ensure all characters are alphanumeric
        $string = preg_replace(array('/\s+/', '/[^A-Za-z0-9\-]/'), array('-', ''), $string);
        // lowercase and trim
        $string = trim(strtolower($string));
        return $string;
    }
    
	private static function transliterate($string) {
        $string = htmlentities(self::utf8ToAscii($string));
        $string = preg_replace(
                        array('/&szlig;/', '/&(..)lig;/', '/&([aouAOU])uml;/', '/&(.)[^;]*;/'),
                        array('ss', "$1", "$1" . 'e', "$1"),
                        $string);

        return $string;
    }
}